/**
 * 
 */
package com.ekingstar.framework.action;

import java.util.ArrayList;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.config.Action;
import org.apache.struts2.config.Result;
import org.apache.struts2.dispatcher.ServletActionRedirectResult;
import org.apache.struts2.interceptor.validation.SkipValidation;

import com.opensymphony.xwork2.ActionSupport;
import com.ekingstar.framework.domain.Function;
import com.ekingstar.framework.domain.FunctionField;
import com.ekingstar.framework.domain.FunctionParameter;
import com.ekingstar.framework.service.FunctionExistException;
import com.ekingstar.framework.service.IFunctionService;

/**
 * 
 * 
 */
@Action(name = "addOrUpdate", namespace = "/function")
@Result(name = "list", value = "list", type = ServletActionRedirectResult.class, params = {
		"namespace", "/function" })
public class AddOrUpdateFunctionAction extends ActionSupport {

	private IFunctionService functionService;

	public void setFunctionService(IFunctionService functionService) {
		this.functionService = functionService;
	}

	private Function function;

	public Function getFunction() {
		return function;
	}

	public void setFunction(Function function) {
		this.function = function;
	}

	public String input() {
		if (function != null && function.getId() != null) {
			function = functionService.viewFunction(function.getId());
		} else if (function.getParent() != null
				&& function.getParent().getId() != null) {
			Function functionParent = functionService.viewFunction(function
					.getParent().getId());
			function = new Function();
			function.setParent(functionParent);
		}
		/*
		if (function.getFields() == null || function.getFields().isEmpty()) {
			function.setFields(new ArrayList());
			for (int i = 0; i < 3; i++) {
				FunctionField ff = new FunctionField();
				function.getFields().add(ff);
			}
		}*/
		return "input";
	}

	//@Validations(requiredStrings = { @RequiredStringValidator(message = "名称必填", fieldName = "function.name") }, regexFields = { @RegexFieldValidator(message = "格式不对", fieldName = "function.addresss", expression = "/.*") })
	public String addOrUpdate() {
		try {
			if (function != null && function.getId() != null
					&& !function.getId().trim().equals("")) {
				functionService.updateFunction(function);
			} else {
				functionService.addFunction(function);

			}
		} catch (FunctionExistException e) {
			this.addActionError(e.getMessage());
			return "input";
		}
		return "list";
	}

	@SkipValidation
	public String addNewField() {
		if (function.getFields() == null || function.getFields().isEmpty()) {
			function.setFields(new ArrayList());
		}
		FunctionField ff = new FunctionField();
		function.getFields().add(ff);
		return "input";
	}

	@SkipValidation
	public String deleteField() {
		String index = ServletActionContext.getRequest().getParameter(
				"deleteFieldIndex");
		if (index != null) {
			int in = Integer.parseInt(index);
			if (function.getFields() != null
					&& function.getFields().size() >= in) {
				function.getFields().remove(in);
			}
		}

		return "input";
	}
	@SkipValidation
	public String addNewParam() {
		if (function.getParams() == null || function.getParams().isEmpty()) {
			function.setParams(new ArrayList());
		}
		FunctionParameter ff = new FunctionParameter();
		function.getParams().add(ff);
		return "input";
	}

	@SkipValidation
	public String deleteParam() {
		String index = ServletActionContext.getRequest().getParameter(
				"deleteFieldIndex");
		if (index != null) {
			int in = Integer.parseInt(index);
			if (function.getParams() != null
					&& function.getParams().size() >= in) {
				function.getParams().remove(in);
			}
		}
		return "input";
	}
}
